function increaseBook()
{
	var nbBook = parseInt($('#nbBook').text());
	$('#nbBook').text(nbBook+1);
	$('#nbBook').css('color','#494949');
	return true;
}

function decreaseBook()
{
	var nbBook = parseInt($('#nbBook').text());
	if (nbBook==0)
		return false;
	if (nbBook==1)
	{
		$('#nbBook').text(0);
		$('#nbBook').css('color','red');
	}
	else
		$('#nbBook').text(nbBook-1);
	return true;
}

function borrowBook()
{
	var textToAdd = $('input#cotations').val();
	if (textToAdd==null || textToAdd=='' || textToAdd=='undefined')
		return false;
	
	if (decreaseBook()==true)
	{
		if ($('select.bookBorrowed').containsOption(textToAdd) == false)
			$('select.bookBorrowed').addOption(textToAdd,textToAdd);
		else
			increaseBook();
		$('input#cotations').val('');
		$('select.bookBorrowed').selectOptions('xxxXXXxxx', true);
		$('input#cotations').focus();
	}
	return false;
}


$(document).ready(function()
	{  
		
		$('select#albumsUser').click(function()
		{
			$(this).copyOptions('select#albumsBack'); 
			$(this).removeOption($(this).selectedValues());
			$('select#albumsBack').selectOptions('xxxXXXxxx', true);
		});
		
		$('select#albumsBack').click(function()
		{
			$(this).copyOptions('select#albumsUser'); 
			$(this).removeOption($(this).selectedValues());
			$('select#albumsUser').selectOptions('xxxXXXxxx', true);
		});
		
		$('strong.retractableB').click(function()
		{
			$('table#tableBorrows').slideToggle(1, function() {
				if ($('table#tableBorrows').css('display')=='block')
					$('table#tableBorrows').css('display', '')
			});
		});
		
		$('strong.retractableR').click(function()
		{
			$('table#tableReturns').slideToggle(1, function() {
				if ($('table#tableReturns').css('display')=='block')
					$('table#tableReturns').css('display', '')
			});
		});
		
		$('form#toBorrow').submit(function()
		{
			return borrowBook();
		});
		
		$('select.bookBorrowed').click(function()
		{
			if( $(this).containsOption(/./) )
			{
				increaseBook();
				$(this).removeOption($(this).selectedValues());
			}
			$('select.bookBorrowed').selectOptions('xxxXXXxxx', true);
			$('input#cotations').focus();
		});
		
		$('input.validReturn').click(function()
		{
			$("#albumsBack").selectOptions(/./, true);
			$('form#bookToReturn').submit();
			return false;
		});
		
		$('input.validBorrow').click(function()
		{
			$("select.bookBorrowed").selectOptions(/./, true);
			$('form#formToBorrow').submit();
			return false;
		});
		
		$("input#cotations").autocomplete(listCotations,{max:10,autoFill: true,minChars: 0});
		$("input#cotations").result(function()
		{
			return borrowBook(); 
		});
		
		$('input#cotations').focus();
		
	}
);